import {
    createSlice
} from "@reduxjs/toolkit";
import {useSelector} from "react-redux";
export type TTabs = {
    label: string,
    key: string,
    closable: boolean
}
const items:TTabs[] = [
    {
        label: "主页",
        key: "/",
        closable: false
    }
]

const commonSlice = createSlice({
    name:"common",
    initialState:{
        items,
        loading:false,
        pageInfo:{
            current:1,// 当前页码
            pageSize:1,// 每页显示的条数
            total:1,//总条数
        }
    },
    reducers:{
        upLoading(state,{payload}){
            state.loading = payload;
        },
        upPageInfo(state,{payload}){
           state.pageInfo = payload;
        },
        addItems(state,{payload}){
            if(!state.items.find((item:TTabs)=>item.key === payload.key))
                state.items.push(payload);
        },
        // delItemsByKey(state,{payload}){
        //     const index = state.items.findIndex((item:TTabs)=>item.key === payload);
        //     state.items.splice(index,1);
        // },
        delItemsByIndex(state,{payload}){
            state.items.splice(payload,1);
        }
    }
})
export const {addItems,upPageInfo,upLoading,delItemsByIndex} = commonSlice.actions;
export const useSelectorCommon = ()=>useSelector((state:any)=>state.common);
export default commonSlice.reducer;